package sphynx;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import sphynx.desl.Argument;
import sphynx.desl.DeslFactory;
import sphynx.desl.DeslPackage;
import sphynx.desl.DeslProgram;
import sphynx.desl.Expression;
import sphynx.desl.proxies.ProxiesPackage;
import sphynx.util.ArgumentSet;

public class ParseDesl {

	public static DeslProgram loadProgram(String input, ResourceSet rs) {
		DeslProgram prog = null;
		
		if (rs == null) rs = new ResourceSetImpl();
		Resource resource = rs.getResource(URI.createFileURI(input), true);
		EObject eobj = resource.getContents().get(0);
		EList<Diagnostic> errors = resource.getErrors();
		
		if (errors.size() > 0) {
			for(Diagnostic err: errors) 
				System.out.printf("Error: %s\n", err.getMessage());
		}
		else 
			prog = (DeslProgram) eobj;
		
		return prog;
	}

	
	/**
	 * @param args
	 * @throws IOException 
	 */
	@SuppressWarnings("unused")
	public static void main(String[] args) throws IOException {
		DeslPackage einstance = DeslPackage.eINSTANCE; // register EistarPackage
		ProxiesPackage proxies = ProxiesPackage.eINSTANCE;
		new DeslStandaloneSetup().createInjectorAndDoEMFRegistration();

		// syntax: ParseDesl [options] <file.desl> arguments
		
		// options
		boolean enterToQuit = false;
		
		int fileIdx = 0;
		String input = null;
		
		
		for(fileIdx = 0; fileIdx < args.length && input == null; fileIdx++) 
			switch (args[fileIdx].charAt(0)) {
			case '/':
			case '-':
				enterToQuit = args[fileIdx].startsWith("p", 1) || args[fileIdx].startsWith("-pause", 1);
				break;
			default:
				input = args[fileIdx];
				break;
			}
		
		if (input == null)
			input = "parseCvsLog.desl";
		
		File fi = new File(input).getAbsoluteFile();
		if (!fi.exists()) {
			if (!input.toLowerCase().endsWith(".desl"))
				fi = new File(input + ".desl");
			if (!fi.exists()) {
				System.out.println("Input file not found.");
				System.exit(1);
			}
		}
		
		// change directory to the input
		File ip = new File(input).getAbsoluteFile();
		//File dir = ip.getAbsoluteFile().getParentFile();
		//System.setProperty("user.dir", dir.getAbsolutePath());
		
		System.out.printf("Compling %s...\n", ip.getAbsolutePath());
		
		ResourceSetImpl rs = new ResourceSetImpl();
		rs.setResourceFactoryRegistry(Resource.Factory.Registry.INSTANCE);
		DeslProgram prg = loadProgram(ip.getAbsolutePath(), rs);
		
		System.out.println("Loading program...");
		
		ArgumentSet opts = new ArgumentSet(new ArrayList<Argument>());
		
		for (int i = fileIdx + 1; i < args.length; i++) {
			String[] param = args[i].split("=", 2);
			Argument argument = DeslFactory.eINSTANCE.createArgument();
			Expression value = DeslFactory.eINSTANCE.createExpression();
			argument.setValue(value);
			if (param.length > 1) {
				argument.setName(param[0]);
				value.setValue(param[1]);
			}
			else
				value.setValue(param[0]);
			opts.getArguments().add(argument);
		}
		try {
			prg.run(null, opts);
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		if (enterToQuit) {
			System.out.println("Type Ctrl-Z to exit.");
			System.in.read();
		}
	}

}
